package com.jsoft.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jsoft.configuration.CustomObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;

import javax.annotation.Resource;
import java.util.Set;

@Component
@Slf4j
public class RedisTemplate {
    @Resource
    private JedisPool jedisPool;
    @Resource
    private CustomObjectMapper objectMapper;

    public String set(String key, String value, long expire) {
        //从连接池中获取jedis对象
        Jedis jedis = jedisPool.getResource();
        String result = null;
        try {
            result = jedis.setex(key, expire, value);
        } catch (JedisException e) {
            log.error("redis execution====>" + e.getMessage());
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = null;
        try {
            result = jedis.get(key);
        } catch (JedisException e) {
            log.error("redis execution====>" + e.getMessage());
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    public String setObject(String key, Object value, long expire) {
        Jedis jedis = jedisPool.getResource();
        String result = null;
        try {
            String jsonValue = objectMapper.writeValueAsString(value);
            result = jedis.setex(key, expire, jsonValue);
        } catch (JsonProcessingException e) {
            log.error("redis execution====>" + e.getMessage());
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    // 根据key去查询redis中对应的value值，对象
    public <T> T getObject(String key, TypeReference<T> typeReference) {
        Jedis jedis = jedisPool.getResource();
        T t = null;
        try {
            // 从redis中读取
            String result = jedis.get(key);
            //转成特定类型
            t = objectMapper.readValue(result, typeReference);
        } catch (JedisException e) {
            log.error("redis execution error----" + e.getMessage());
            // 如果出现了异常，要把jedis连接释放
            jedisPool.returnResource(jedis);
        } catch (JsonProcessingException e) {
            log.error("json error----" + e.getMessage());
            jedisPool.returnResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return t;
    }

    //根据key值删除
    public Long remove(String... key) {
        Jedis jedis = jedisPool.getResource();
        Long result = 0L;
        try {
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("json error----" + e.getMessage());
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;

    }

    /*
     * token续命，每次操作的都增加token的时间
     * */
    public Long expire(String key, Long expire) {
        Jedis jedis = jedisPool.getResource();
        Long exp = -1L;
        try {
            exp = jedis.expire(key, expire);
        } catch (Exception e) {
            log.error("json error----" + e.getMessage());
            jedisPool.returnResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return exp;
    }
    /*
     *取出符合的key
     *  */
    public Set<String> keys(String pattern){
        Jedis jedis = jedisPool.getResource();
        Set<String> set = null;
        try {
            set = jedis.keys(pattern);
        } catch (Exception e) {
            log.error("json error----" + e.getMessage());
            jedisPool.returnResource(jedis);
        }finally{
            jedisPool.returnResource(jedis);
        }
        return set;
    }


}
